<template>
  <div>
    <card title="你的故事">
      <div>
        <div id="divTitle" ref="editTitleRef" content="请输出标题"></div>
        <div id="divContent" ref="editContentRef"></div>
        <div class="button">
          <el-button
            type="warning"
            plain
            @click="clearContent"
            icon="el-icon-refresh"
            >重置</el-button
          >
          <el-button type="primary" @click="submitData" icon="el-icon-position"
            >提交</el-button
          >
        </div>
      </div>
    </card>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import card from '../../../../../../base-ui/card'
import { newStory } from '../../../../../../service/mian/system/system'
import E from 'wangeditor'
export default defineComponent({
  name: 'chat',
  components: {
    card
  },
  setup() {
    const editContentRef = ref()
    const editorContent = ref()
    const editTitleRef = ref()
    const editorTitle = ref()
    onMounted(() => {
      // 标题
      editorTitle.value = new E(editTitleRef.value)
      editorTitle.value.config.height = 50
      editorTitle.value.config.menus = ['bold', 'head', 'italic']
      editorTitle.value.config.placeholder = '请输入标题'
      editorTitle.value.create()
      // 内容
      editorContent.value = new E(editContentRef.value)
      editorContent.value.create()
    })
    const submitData = () => {
      const Title = editorTitle.value.txt.text() //标题
      const Content = editorContent.value.txt.html() //内容
      const data = {
        title: Title,
        content: Content
      }
      if (Title && Content) {
        newStory('/story', data).then(() => {
          ElMessage.success({
            message: '创建故事成功，请在故事列表中查看',
            type: 'success'
          })
        })
      } else {
        ElMessage.error('创建故事失败，请请填写完整字段')
      }
    }
    const clearContent = () => {
      editorTitle.value.txt.clear()
      editorContent.value.txt.clear()
    }
    return { editContentRef, editTitleRef, submitData, clearContent }
  }
})
</script>

<style scoped>
.button {
  text-align: right;
  margin-top: 20px;
}
#divTitle {
  text-align: left;
}
#divContent {
  margin-top: 30px;
  text-align: left;
}
</style>
